<?php

namespace Proj\ResManBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Proj\ResmanBundle\Helper\GenerationHelper;

/**
 * @Route("/public", name="public_index")
 */
class PublicController extends Controller {
    
     /**
     * @Route("/schedule")
     * @Template()
     */
    public function showScheduleAction(Request $request) {
        $urlUbb = "http://www.cs.ubbcluj.ro/files/orar/2012-2/cadre/agoc.html";
        $contents = file_get_contents($urlUbb);

        $schedule = GenerationHelper::generateScheduleFrom($contents);
        $baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath() . "/app_dev.php";

        $content_array = array(
            "schedule" => $schedule,
            "submenu" => $this->getLinksArray($baseurl)
        );

        return $this->getTemplateContent($baseurl, $content_array);
    }

    /**
     * @Route("/generate/schedule")
     */
    public function generateScheduleAction() {
        $urlUbb = "http://www.cs.ubbcluj.ro/files/orar/2012-2/cadre/agoc.html";
        $contents = file_get_contents($urlUbb);

        $schedule = GenerationHelper::generateScheduleFrom($contents);
        $baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath() . "/app_dev.php";
        
        $content_array = array(
            "schedule" => $schedule,
            "submenu" => $this->getLinksArray($baseurl)
        );

        return $this->getTemplateContent($baseurl, $content_array);
    }
    
    /**
     * @Route("/departments", name="public_departments")
     */
    public function getDepartmentsAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $departments = $em->getRepository('ProjResManBundle:Department')->findAll();
        
        $baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath() . "/app_dev.php";
        $content_array = array("submenu" => $this->getLinksArray($baseurl));
        
        return $this->render(
            'ProjResManBundle:Public:get_departments.html.twig',
            array('departments' => $departments,
                  'template_content' => $this->getTemplateContent($baseurl, $content_array)
            )
        );
    }
     /**
     * @Route("/departments/logisticalresources", name="public_department_logisticalresources")
     */
    public function getDepartmentLogisticalResourcesAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $id = $this->getRequest()->query->get('department_id');
        $department= $em->getRepository('ProjResManBundle:Department')->find($id);
        $resources = $department->getResources();
        
        $baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath() . "/app_dev.php";
        $content_array = array("submenu" => $this->getLinksArray($baseurl));
        
        return $this->render(
            'ProjResManBundle:Public:get_department_logisticalresources.html.twig',
            array('resources' => $resources,
                  'template_content' => $this->getTemplateContent($baseurl, $content_array)    
            )
        );
    }
    
     /**
     * @Route("/departments/humanresources", name="public_department_humanresources")
     */
    public function getDepartmentHumanResourcesAction(Request $request) {
        $em = $this->getDoctrine()->getManager();
        $id = $this->getRequest()->query->get('depatment_id');
        $department = $em->getRepository('ProjResManBundle:Department')->find($id);
        $persons = $department->getPersons();
        
        $baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath() . "/app_dev.php";
        $content_array = array("submenu" => $this->getLinksArray($baseurl));
        
        return $this->render(
            'ProjResManBundle:Public:get_department_humanresources.html.twig',
            array('persons' => $persons,
                  'template_content' => $this->getTemplateContent($baseurl, $content_array) 
            )
        );
    }
    
     /**
     * @Route("/researchprojects", name="public_research_projects")
     * @Route("/")
     */
    public function researchProjectsAction(Request $request) {
        
        $em = $this->getDoctrine()->getManager();
        $projects = $em->getRepository('ProjResManBundle:Project')->findBy(array('type' => 0));
       
        $baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath() . "/app_dev.php";
        $content_array = array("submenu" => $this->getLinksArray($baseurl));
        
        return $this->render(
            'ProjResManBundle:Public:research_projects.html.twig',
            array('projects' => $projects,
                  'template_content' => $this->getTemplateContent($baseurl, $content_array)
            )
        );
    }
    
    
    /**
     * @Route("/researchprojects/activities", name="public_research_projects_activities")
     */
    public function researchProjectsActivitiesAction() {
        $em = $this->getDoctrine()->getManager();
        $projects = $em->getRepository('ProjResManBundle:Project')->findBy(array('type' => 0));

        $activities = array();
        
        $id = $this->getRequest()->query->get('project_id');
        if ($id != null)
        {
            $project = $em->getRepository('ProjResManBundle:Project')->find($id);
            $activities = $project->getActivities();
        }
        
        return $this->render(
            'ProjResManBundle:Public:research_projects.html.twig',
            array('projects' => $projects)
        );
    }
    
    private function getTemplateContent($baseurl, $content_array) {
        
        $securityContext = $this->container->get('security.context');
        $token = $securityContext->getToken();
        $user = $token->getUser();
        $roles = "";

        $menu = array(
            "public" => "$baseurl/public/index",
            "admin" => "$baseurl/admin/index",
            "director" => "$baseurl/director/index",
            "cadre" => "$baseurl/cadre/index",
            "login" => "$baseurl/login",
            "logout" => "$baseurl/logout",
        );

        $text = array(
            "login" => "Log-in",
            "logout" => "Log-out",
        );

        $auth = array(
            "user" => $user,
            "isAuth" => $securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED'),
            "roles" => $roles
        );

        $header_content = array(
            "menu" => $menu,
            "text" => $text
        );

        $template_content = array(
            "header" => $header_content,
            "content" => $content_array,
            "footer" => array(),
            "auth" => $auth
        );
        
        return $template_content;
    }
    
    private function getLinksArray($baseurl){
        return array (
            1 => array("href" => "$baseurl/public/researchprojects", 
                       "title" => "Proiecte Stiintifice",
                       "title_desc" => "Vizualizare proiecte stiintifice"),
            2 => array("href" => "$baseurl/public/departments", 
                       "title" => "Resurse",
                       "title_desc" => "Resurse logistice si umane"),
            3 => array("href" => "$baseurl/public/schedule", 
                       "title" => "Calendar",
                       "title_desc" => "Calendarul activitatilor"),
        );
    }
}